package DML;

import java.util.ArrayList;
import java.util.List;

import DuiXiang.TableModel;

public class UpdateTableParsing {
	public static int updateTableParsing(String sql){
		if (sql.charAt(sql.length()-1) != ';')
			return -4;												//语法错误
		
		int i,j;
		int number1=0,number2=0;
		boolean flag=true;
		List<String> name = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		List<String> setName = new ArrayList<String>();
		List<String> setValues = new ArrayList<String>();
		
		for(i=0;i<sql.length()-1;i++){
			if(sql.charAt(i)=='(')
				number1++;
			if(sql.charAt(i)==')')
				number2++;
		}
		if(number1!=1||number2!=1)
			return -4;
		String[] sqlsplit = sql.split("\\s+");
		
		String dataBaseName = sqlsplit[sqlsplit.length-1].substring(0,sqlsplit[sqlsplit.length-1].length()-1);
		String tableName = sqlsplit[1];
		String dbfPath = "D:\\database\\"+dataBaseName+ "\\"+ tableName + ".dbf";
		List<TableModel> tableModelList = util.ReadTableModelList(dbfPath);
		if(tableModelList==null)
			return -1;               									//无此表或数据库
		if(!sqlsplit[2].equals("(SET")&&!sqlsplit[2].equals("(set"))
			return -4;													//语法错误
		if(!sqlsplit[4].equals("WHERE")&&!sqlsplit[4].equals("where")){
			System.out.println("--");
			return -4;													//语法错误
		}
		if(!sqlsplit[sqlsplit.length-2].equals("IN")&&!sqlsplit[sqlsplit.length-2].equals("in"))
			return -4;													//语法错误
		
		String[] setsplit = sqlsplit[3].split(",");
		for(String ss:setsplit) {
			String[] temp = ss.split("=");
			if(temp.length!=2)
				return -4;												//语法错误
			for (j = 0; j < tableModelList.size(); j++) {
				if (tableModelList.get(j).getsFieldName().equals(temp[0]))
					break;
			}
			if (j == tableModelList.size())
				return -2;                        					   //无该字段
			for(TableModel tt:tableModelList)
				if((tt.getsFieldName()).equals(temp[0])){
					switch(tt.getsType()){
					case "int":
					case "long":
						for (j = 0; j < temp[1].length(); j++)
							if (temp[1].charAt(j) < '0'
									|| temp[1].charAt(j) > '9')
								break;
						if (j < temp[1].length())
							return -3; // 格式不匹配
						break;
					case "float":
						for (j = 0; j < temp[1].length(); j++) {
							if (temp[1].charAt(j) >= '0'
									&& temp[1].charAt(j) <= '9')
								continue;
							else if (temp[1].charAt(j) == '.' && flag) {
								flag = false;
								continue;
							} else
								break;
						}
						if (j < temp[1].length())
							return -3; // 格式不匹配
						break;
					case "char":
					case "varchar":
						if (temp[1].charAt(0) != '\''
								|| temp[1].charAt(temp[1].length() - 1) != '\'')
							return -3; // 格式不匹配
						else
							temp[1]=temp[1].substring(1,temp[1].length()-1);
						break;
					default:
						break;
					}
					break;
				}
			setName.add(temp[0]);
			setValues.add(temp[1]);
		}
		sqlsplit[sqlsplit.length-3]=sqlsplit[sqlsplit.length-3].substring(0,sqlsplit[sqlsplit.length-3].length()-1);
		for(i=5;i<(sqlsplit.length-2);i++){
			String[] temp = sqlsplit[i].split("=");
			if(temp.length!=2)
				return -4;												//语法错误
			for (j = 0; j < tableModelList.size(); j++) {
				if (tableModelList.get(j).getsFieldName().equals(temp[0]))
					break;
			}
			if (j == tableModelList.size())
				return -2;                       					   //无该字段
			for(TableModel tt:tableModelList)
				if((tt.getsFieldName()).equals(temp[0])){
					switch(tt.getsType()){
					case "int":
					case "long":
						for (j = 0; j < temp[1].length(); j++)
							if (temp[1].charAt(j) < '0'
									|| temp[1].charAt(j) > '9')
								break;
						if (j < temp[1].length())
							return -3; // 格式不匹配
						break;
					case "float":
						for (j = 0; j < temp[1].length(); j++) {
							if (temp[1].charAt(j) >= '0'
									&& temp[1].charAt(j) <= '9')
								continue;
							else if (temp[1].charAt(j) == '.' && flag) {
								flag = false;
								continue;
							} else
								break;
						}
						if (j < temp[1].length())
							return -3; // 格式不匹配
						break;
					case "char":
					case "varchar":
						if (temp[1].charAt(0) != '\''
								|| temp[1].charAt(temp[1].length() - 1) != '\'')
							return -3; // 格式不匹配
						else
							temp[1]=temp[1].substring(1,temp[1].length()-1);
						break;
					default:
						break;
					}
					break;
				}
			name.add(temp[0]);
			values.add(temp[1]);
			if (i + 1 < (sqlsplit.length - 2)) {
				i++;
				if (!sqlsplit[i].equals("AND") && !sqlsplit[i].equals("and")) {
					
					System.out.println(sqlsplit[i]+"6");
					return -4; // 语法错误
				}
			}
		}
		int result = UpdateTable.updateTable(dataBaseName, tableName, setName, setValues, name, values);
		
		return result;
	}
}
